package com.net.netmonitor.util;

import org.springframework.util.ResourceUtils;

import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class FrameUI extends JPanel {
    private static final int JFRAME_WIDTH = 350;
    private static final int JFRAME_HEIGHT= 400;
    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    //内容面板，由JFrame获取，所有信息展示在此panel上
    private static JPanel contentPanel;

    public  void createWindow() throws Exception{

        String ROOT = PropertiesFileUtil.getRootPath();

        //===============窗口-顶层面板
        JFrame jFrame = new JFrame("网速监测");
        //设置窗口关闭按钮，退出程序
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //===============网格布局,容器中的方法
        GridBagLayout gridBagLayout = new GridBagLayout();
        setLayout(gridBagLayout);
        jFrame.add(this,BorderLayout.CENTER);
        jFrame.setSize(JFRAME_WIDTH,JFRAME_HEIGHT);
        //设置窗口的icon，左上角
        ImageIcon icon = new ImageIcon(ROOT + "\\images\\icon.jpg");
        jFrame.setIconImage(icon.getImage());

        //===============弹框位置：居中
        Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
        jFrame.setLocation((int)(dimension.getWidth() - JFRAME_WIDTH ) / 2,(int)(dimension.getHeight() - JFRAME_HEIGHT ) / 2);

        //===============设置IFrame的背景图片
        //1.获取图片路径
        String image = ROOT + "\\images\\bg2.jpg";
        ImageIcon img_1 = new ImageIcon(image);// 背景图片
        JLabel label = new JLabel(img_1);// 把背景图片显示在一个标签里面
        //2.创建label，展示图片
        label.setBounds(0, 0, JFRAME_WIDTH,JFRAME_HEIGHT);// 把标签的大小位置设置为图片刚好填充整个面板
        //3.把内容窗格转化为JPanel，否则不能用方法setOpaque()来使内容窗格透明
        contentPanel = (JPanel) jFrame.getContentPane();
        contentPanel.setOpaque(false);
        contentPanel.setSize(JFRAME_WIDTH,JFRAME_HEIGHT);
        //4.内容窗格默认的布局管理器为BorderLayout，修改为gridBagLayout
        contentPanel.setLayout(gridBagLayout);
//            jFrame.getLayeredPane().setLayout(null);
        //5.把背景图片添加到分层窗格的最底层作为背景
        jFrame.getLayeredPane().add(label, new Integer(Integer.MIN_VALUE));

        //===============初次打开面板，初始化信息时显示提示消息
        JLabel tipLabel = new JLabel("正在获取本机CPU等信息，请稍等...");
        tipLabel.setOpaque(false);
        contentPanel.add(tipLabel);

        //===============展示面板，并且不可编辑
        jFrame.setVisible(true);
        jFrame.setResizable(false);

        while (true){
            //当前网速
            JLabel lable_net_key = new JLabel("当前网速:");
            lable_net_key.setOpaque(false);
            String net = IperfUtil.getNetSpeed();
            String net_val = net + "MB/s";
            JLabel lable_net_val = new JLabel(net_val);
            lable_net_val.setOpaque(false);

            //当前cpu
            JLabel lable_cpu_key = new JLabel("CPU:");
            lable_cpu_key.setOpaque(false);
            double cpu = ComputerMonitorUtil.getCpuUsage();
            double cpuRate = Math.round(cpu) / (double)100;
            String cpu_val = Double.toString(cpu) + "%";
            JLabel lable_cpu_val = new JLabel(cpu_val);
            lable_cpu_val.setOpaque(false);

            //当前内存
            JLabel lable_mem_key = new JLabel("内存:");
            lable_mem_key.setOpaque(false);
            double mem = ComputerMonitorUtil.getMemUsage();
            double memRate = Math.round(mem) / (double)100;
            String mem_val = Double.toString(mem) + "%";
            JLabel lable_mem_val = new JLabel(mem_val);
            lable_mem_val.setOpaque(false);

            //当前磁盘
//                JLabel lable_disk_key = new JLabel("磁盘:");
//                String disk_val = Double.toString(ComputerMonitorUtil.getDiskUsage()) + "%";
//                JLabel lable_disk_val = new JLabel(disk_val);

            //当前时间
            JLabel lable_time_key = new JLabel("获取时间:");
            lable_time_key.setOpaque(false);
            String time_val = sdf.format(new Date());
            JLabel lable_time_val = new JLabel(time_val);
            lable_time_val.setOpaque(false);


//                System.out.println(cpu_val);
//                System.out.println(mem_val);
//                System.out.println(disk_val);

            //移除所有组件，重新加载
            contentPanel.removeAll();

            //网格布局
            GridBagConstraints gridBagConstraints = new GridBagConstraints();
            gridBagConstraints.fill = GridBagConstraints.NONE;
            gridBagConstraints.anchor = GridBagConstraints.CENTER;
            gridBagConstraints.weightx = 3;
            gridBagConstraints.weighty = 4;
            //添加组件并指定组件的位置
            addComponent(lable_net_key,gridBagConstraints,0,1,1,1);
            addComponent(lable_net_val,gridBagConstraints,1,1,1,1);
            addComponent(lable_cpu_key,gridBagConstraints,0,2,1,1);
            addComponent(lable_cpu_val,gridBagConstraints,1,2,1,1);
            addComponent(lable_mem_key,gridBagConstraints,0,3,1,1);
            addComponent(lable_mem_val,gridBagConstraints,1,3,1,1);
//                addComponent(lable_disk_key,gridBagConstraints,0,4,1,1);
//                addComponent(lable_disk_val,gridBagConstraints,1,4,1,1);
            addComponent(lable_time_key,gridBagConstraints,0,4,1,1);
            addComponent(lable_time_val,gridBagConstraints,1,4,1,1);

            jFrame.validate();
            jFrame.repaint();

            //将数据同步到数据库，可以注释掉
//            updateData(net,cpuRate,memRate);
        }

    }

    //将数据同步到数据库
    public void updateData(String bandWidth,double cpuRate,double memRate){

        try{
            String ROOT = PropertiesFileUtil.getRootPath();
            String filePath = ROOT + "\\serverInfo.properties";
            //采集服务器部署访问接口的前缀
            String URL_PREFIX = PropertiesFileUtil.getValueByKey("uecollectserverurl", filePath);
            String url = URL_PREFIX + "/ue/rest/clientSync/saveMonitorMachine";

            //客户端id
            String machineId = PropertiesFileUtil.getValueByKey("machineId", filePath);

            Map<String,Object> param = new HashMap();
            param.put("id", "");
            param.put("machineId", machineId);
            param.put("ipAddr", "");
            param.put("provinceId", "");
            param.put("bandWidth", bandWidth);
            param.put("cpuRate", cpuRate);
            param.put("memRate", memRate);
            param.put("lastModified", "");
            param.put("creatDate", "");

            HttpClientUtil.postRequest(url, param);
        }catch (Exception e){
            e.printStackTrace();;
        }
    }

    //抽离的公共方法，在指定位置添加组件
    private void addComponent(Component component,GridBagConstraints gridBagConstraints,int x,int y,int w,int h){
        gridBagConstraints.gridx = x;
        gridBagConstraints.gridy = y;
        gridBagConstraints.gridwidth = w;
        gridBagConstraints.gridheight = h;
        //Container中的方法
        contentPanel.add(component,gridBagConstraints);
    }
}
